using System.Drawing;
using System.Windows.Forms;
using CloudShot.Editor.CanvasFigures;
using CloudShot.Entities;
using CloudShot.Properties;
using CloudShot.Utils;

namespace CloudShot.Editor.Tools.States
{
  internal class ResizeObjectState : IPointerToolState
  {
    private readonly IDrawingCanvas _canvas;
    private CanvasObject _resizedObject;
    private readonly int _resizedObjectHandle;

    public ResizeObjectState(IDrawingCanvas canvas, CanvasObject resizedObject, int resizedObjectHandle)
    {
      _resizedObject = resizedObject;
      _resizedObjectHandle = resizedObjectHandle;
      _canvas = canvas;
    
      resizedObject.CurrentDragHandle = resizedObjectHandle;
      resizedObject.IsShowSize = true;

      // Since we want to resize only one object, deselect all other objects
      _canvas.GraphicsList.DeselectAll();
      _resizedObject.IsSelected = true;

    }

    #region IPointerToolState Members

    public void OnMouseMove(Point point)
    {
      if (_resizedObject == null)
        return;

      using (new AutomaticCanvasRefresher(_canvas, () => _resizedObject.GetBoundingBox()))
      {
        _resizedObject.MoveHandleTo(point, _resizedObjectHandle, _canvas.IsAnyModifierPressed(KeyModifiers.Shift));  
      }
    }

  

    public void Enter()
    {
      string hint = HintTextHelper.GetResizeHint(_resizedObject, _resizedObjectHandle);
      if (!string.IsNullOrEmpty(hint))
        _canvas.ShowHint(hint);
    }

    public void Release()
    {
      if (_resizedObject != null)
      {
        if (!string.IsNullOrEmpty(HintTextHelper.GetResizeHint(_resizedObject, _resizedObjectHandle)))
          _canvas.HideHint();

        using (new AutomaticCanvasRefresher(_canvas, () => _resizedObject.GetBoundingBox()))
        {
          _resizedObject.IsShowSize = false;
          // after resizing
          _resizedObject.Normalize();
        }

        _resizedObject = null;
      }
    }

    public void OnKeyDown(KeyEventArgs keyEventArgs)
    {
      //Empty
    }

    public void OnKeyUp(KeyEventArgs keyEventArgs)
    {
      //Empty
    }

    #endregion
  }
}